'''
Created on Sep 2, 2012

@author: Jason Huang
'''

MSGS = (
    #CT 1
    '315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021' +
    'dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8c' + 
    'c57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d' +
    '524400a19b159610b11ef3e',
    
    #CT 2
    '234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef408' +
    '1ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d55' +
    '1f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f',
    
    #CT 3
    '32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e' +
    '51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de8123' +
    '0b59b7afb5f41afa8d661cb',
    
    #CT 4
    '32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef50' +
    '21eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee4160e' +
    'ad45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b130f040d8ec6447e2c767f3d30ed8' +
    '1ea2e4c1404e1315a1010e7229be6636aaa',
    
    #CT 5
    '3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee02' +
    '1dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81230b59' +
    'b73fb4302cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722130f042f8ec85b7c2070',
    
    #CT 6
    '32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be8' +
    '4d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79' +
    'eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c' +
    '4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d2' +
    '5f8e4',
    
    #CT 7
    '32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c' +
    '04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b' +
    '4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2' +
    'e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f' +
    '9b13f1efff71ea313c8661dd9a4ce',
    
    #CT8
    '315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40' +
    'c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95' +
    'bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3',
    
    #CT 9
    '271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc' +
    '5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9' +
    'cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a' +
    '2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027',
    
    #CT 10
    '466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d03' +
    '15fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559' +
    'ba520e89a2cb2a83',
    
    #target
    '32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14' +
    'd13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904'
    )

N = len(MSGS[-1])

byteArr = [bytes.fromhex(ct[:N]) for ct in MSGS]
keyArr = [0x66, 0x39, 0x6e, 0x89, 0xc9, 0xdb, 0xd8, 204 , 0x98, 0x74, 
          0x35, 0x2a, 0xcd, 0x63, 0x95, 0x10, 0x2e, 0xaf, 0xce, 0x78, 
          0xaa, 0x7f, 0xed, 0x28, 0xa0, 127,  0x6b, 0xc9, 0x8d, 0x29,
          0xc5, 0x0b, 0x69, 0xb0, 0x33, 0x9a, 0x19, 0xf8, 0xaa, 64, 
          26,   156,  0x6d, 0x70, 0x8f, 0x80, 0xc0, 0x66, 0xc7, 0x63,
          0xfe, 0xf0, 0x12, 0x31, 0x48, 0xcd, 0xd8, 0xe8, 0x02, 0xd0,
          0x5b, 0xa9, 0x87, 0x77, 0x33, 0x5d, 0xae, 0xfc, 0xec, 0xd5,
          0x9c, 0x43, 0x3a, 0x6b, 0x26, 0x8b, 0x60, 0xbf, 0x4e, 0xf0,
          0x3c, 0x9a, 97,
          ]
    
def isLetter(num):
    return (0x41 <= num and num <= 0x5A) or (0x61 <= num and num <= 0x7A) or num == 0x20 or num == 0x28 or num == 0x29 or num == 0x2c or num == 0x2d or num == 0x2e or num == 0x3a or (0x30 <= num and num <= 0x39)


def check(idx, arr):
    
    trialKey = arr[idx] ^ 0x20
    for i in range(len(arr)):
        if i == idx:
            continue
        if not isLetter(trialKey ^ arr[i]):
            return False
    return True


def solve():
    
    for i,b in enumerate(byteArr[-1]):
        
        arr = [a[i] for a in byteArr]
        
        if(i == 82):
            print(arr)
            exit()
        
        if(i < len(keyArr)):
            continue
        
        done = False
        for i1 in range(len(arr) - 1):
            if done:
                break
            for i2 in range(i1+1, len(arr)):
                if isLetter(arr[i1] ^ arr[i2]):
                    if check(i1, arr):
                        keyArr.append(arr[i1] ^ 0x20)
                        done = True
                    elif check(i2, arr):
                        keyArr.append(arr[i2] ^ 0x20)
                        done = True
                
                if done:
                    print(hex(keyArr[-1]))
                    print(chr(keyArr[-1] ^ b))
                    break
                
        if not done:
            print("key not found at ", i)
            print(arr)
    
    return keyArr,byteArr[-1]
    


if __name__ == '__main__':
    #solve()
    
    for i,ct in enumerate(byteArr):
        for k,b in zip(keyArr, ct):
            try:
                print(chr(k^b), end='')
            except UnicodeEncodeError:
                print('?', end='')
        print()
    
